// $('#datetime').datepicker({
//     format: 'yyyy-mm-dd'
// });
$('#datetime-1').datepicker({
    format: 'yyyy-mm-dd'
}).on('changeDate', function (e) {
    var date = new Date($("#datetime-1").val().replace(/-/, "/").replace("-", "/"));
    //计算相差多少天
    var d = new Date();
    var time = d.getTime() - date.getTime();
    var days = parseInt(time / (1000 * 60 * 60 * 24));
    if (days > 15) {
        layer.msg("结束日期不能超过当前日期15天");
        $("#datetime-1").val("");
        $("#datetime").val("");
        return;
    }

    var weekDay = [7, 1, 2, 3, 4, 5, 6];
    if (weekDay[date.getDay()] != 5) {
        layer.msg("结束日期必须为星期五");
        $("#datetime-1").val("");
        $("#datetime").val("");
        return;
    } else {
        //开始日期为结束日期的前6天
        var now = new Date(date.getTime() - 86400000 * 6);
        var year = now.getFullYear(),
            month = (now.getMonth() + 1).toString(),
            day = now.getDate().toString();
        if (month.length == 1) {
            month = '0' + month;
        }
        if (day.length == 1) {
            day = '0' + day;
        }
        var datetime = (year + '-' + month + '-' + day);
        $("#datetime").val(datetime);
        //计算剩余投入占比
        $certusUtils.ajax('findByDateTime', { "time": $("#datetime-1").val() }, function (result) {
            var syInput = 100;
            syInput = syInput - result.data;
            $$scope.syInput = syInput;
            $("#inPercent").attr("placeholder", "剩余投入百分比：" + syInput);
        });

    }
});
function addPlan() {
    // var data = new Date();
    // var datetime = $("#datetime").val();
    // if (datetime!=""){
    //     var startTime = new Date(datetime.replace("/", "-").replace("/", "-"));
    //     if (startTime>data){
    //         layer.msg("开始日期不能大于当前日期",{time:1000});
    //         return;
    //     }
    // }else{
    //     layer.msg("开始日期不能为空",{time:1000});
    //     return;
    // }
    var beginTime = $("#datetime").val();
    if (beginTime == "") {
        layer.msg("开始日期不能为空", { time: 1000 });
        return;
    }

    var endTime = $("#datetime-1").val();
    if (endTime == "") {
        layer.msg("结束日期不能为空", { time: 1000 });
        return;
    }
    // if ($("#krWcl").val()=="100"){
    //     layer.confirm('当前完成率为100%，是否确定任务已完成？', {
    //         btn: ['确定', '取消'] //按钮
    //     }, function () {
    //
    //     });
    // }

    $certusUtils.ajax("isWrite", { "beginTime": beginTime, "endTime": endTime, "krsId": ksid }, function (response) {
        if (response.data > 0) {
            layer.msg("请勿重复填写进展");
            return;
        } else {
            if ($("#krWcl").val() == "") {
                layer.msg("任务完成率不能为空", { time: 1000 });
                return;
            }
            if ($("#inPercent").val() == "") {
                layer.msg("实际投入百分比不能为空", { time: 1000 });
                return;
            }
            if (Number($("#inPercent").val()) > $$scope.syInput) {
                layer.msg("实际投入百分比不能大于" + $$scope.syInput, { time: 1000 });
                return;
            }

            if ($("#jzv").val() == "") {
                layer.msg("目标进展和困难不能为空", { time: 1000 });
                return;
            }
            if ($("#nextPlan").val() == "") {
                layer.msg("下一步计划不能为空", { time: 1000 });
                return;
            }
            if ($("#jzv").val().length > 500) {
                layer.msg("目标进展和困难不能超过500字", { time: 1000 });
                return;
            }
            if ($("#nextPlan").val().length > 500) {
                layer.msg("下一步计划不能超过500字", { time: 1000 });
                return;
            }

            var arr = $("form").serializeArray();
            var data = {};
            data.krsId = ksid;
            for (var i = 0; i < arr.length; i++) {
                data[arr[i].name] = arr[i].value;
            };
            data.startTime = beginTime;
            $certusUtils.ajax("addFare", { "params": JSON.stringify(data) }, function (result) {
                var data = result.data;
                if (data == true) {
                    layer.msg("添加成功", { time: 500 });
                    // findHistPlans(ksid);
                    toLoadHtml(4, 5);
                } else {
                    layer.msg("添加失败", { time: 500 })
                }
            });
        }
    });



    // var threeDay = getBeforeDate(3);
    // if (endTime>threeDay){
    //     layer.msg("结束日期不能超过当前日期后三天");
    //     return;
    // }
}

//n为你要传入的参数，当前为0，前一天为-1，后一天为1
function getBeforeDate(n) {
    var date = new Date();
    var year, month, day;
    date.setDate(date.getDate() + n);
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = date.getDate();
    s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
    return s;
}
// OKR子任务进展
var childLogColumns = [{
    id: 'o_desc',
    title: '阶段目标',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'temp_desc',
    title: '标准任务',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'krs_desc',
    title: '关键成果',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'rw_desc',
    title: '子任务进展',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
}, {
    id: 'next_plan',
    title: '下一步计划',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
}, {
    id: 'finshtime',
    title: '计划完成时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'task_fzuser',
    title: '成果负责人',
    type: 'string',
    width: 80,
    columnClass: 'text-center',
}, {
    id: 'kr_weight',
    title: 'KR权重',
    type: 'string',
    width: 80,
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
}, {
    id: 'kr_wcl',
    title: 'KR完成率',
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
}, {
    id: 'in_percent',
    title: '实际投入百分比',
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
}, {
    id: 'input_weight',
    title: '投入百分比',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
}, {
    id: 'finish_cycle',
    title: '完成周期',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'updateTime',
    title: '更新时间',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
}, {
    id: 'operation',
    title: '操作',
    width: 80,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        //计算相差多少天
        var d = new Date();
        start = record.updateTime.replace(/-/g, "/");
        var startdate = new Date(start);
        var time = d.getTime() - startdate.getTime();
        var days = parseInt(time / (1000 * 60 * 60 * 24));
        //大于三天则不能修改和删除
        if (days <= 3) {
            content += '<button class="btn btn-xs btn-danger" onclick="remove(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        }
        return content;
    }
}];
var ksid = "";
// var fintime="";
function findHistPlans(kid) {
    ksid = kid;
    $("#childLogContainer").empty();
    $("#childLogToolBarContainer").empty();
    $CertusJsonAjax.dataInit("findOkrPlanList", { "kid": kid }, childLogColumns, 'childLogContainer', 'childLogToolBarContainer');

    // $certusUtils.ajax("findFinishTime",{"kid":kid},function (response) {
    //     fintime = response.data;
    // })
}

function remove(id) {
    $certusUtils.ajax("findDpCount", { "id": id }, function (response) {
        if (response.data > 0) {
            layer.msg("当前进展存在点评数据不能删除");
            return;
        } else {
            layer.confirm('确认删除此数据？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $certusUtils.ajax('delPlan', { "id": id }, function () {
                    layer.msg("删除成功");
                    findHistPlans(ksid);
                });
            })
        }
    });
}

// function edit(id) {
//     $certusUtils.ajax('findPlan',{"id":id},function (response) {
//        var data = response.data;
//        $("#datetime").val(data.beginTime);
//        $("#datetime-1").val(data.endTime);
//        $("#krWcl").val(data.krWcl);
//        $("#inPercent").val(data.inPercent);
//        $("#jzv").val(data.rwDesc);
//        $("#nextPlan").val(data.nextPlan);
//     });
// }

function back() {
    toLoadHtml(4, 5);
}